package controllers;

import play.*;
import play.mvc.*;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import models.*;
//@With(Secure.class)
public class Application extends Controller {
    public static void index() {
    	List<Question> elections=Question.findAll();
    	User user=Security.getConnected();
//System.out.println(user.email);
List<Question> voted=new ArrayList<Question>();
List<Question> nvoted=new ArrayList<Question>();
if(user!=null){
for(Question q:elections){
	for(Answer a:user.answers){
		if(a.question.id==q.id){
			voted.add(q);
		}
		
	}
	
}}
boolean thereended=false;
for(Question qq:elections){
	if(!voted.contains(qq)){
		nvoted.add(qq);
	}
	if(qq.isEnded){
		thereended=true;
	}
	
}

render(thereended,elections,voted,nvoted, user);
    }

		public static String random_hash( int length )
	{
		return hash( System.currentTimeMillis() * Math.random() + "" ).substring( 0, length );
	}
		public static String hash( String str )
		{
			String result = "";
			try
			{
				MessageDigest algorithm = MessageDigest.getInstance( "MD5" );
				algorithm.reset();
				algorithm.update( str.getBytes() );
				byte[] md5 = algorithm.digest();
				String tmp = "";
				for( int i = 0; i < md5.length; i++ )
				{
					tmp = (Integer.toHexString( 0xFF & md5[i] ));
					if( tmp.length() == 1 )
					{
						result += "0" + tmp;
					}
					else
					{
						result += tmp;
					}
				}
			}
			catch( NoSuchAlgorithmException ex )
			{
			}
			return result;
		}



}